/*   
* Project: OSMP
* FileName: DataController.java
* version: V1.0
*/
package com.cnpc.iot.data.controller;

import com.cnpc.framework.base.pojo.Result;
import com.cnpc.framework.base.service.BaseService;
import com.cnpc.framework.utils.FreeMarkerUtil;
import com.cnpc.framework.utils.PropertiesUtil;
import com.cnpc.iot.scene.entity.Device;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Description:
 * 
 * @author: wangkaiping
 * @date: 2018年2月4日 上午10:41:29上午10:51:30
 */
@Controller
@RequestMapping("/data")
public class DataController {

    @Resource
    private BaseService baseService;

	@RequestMapping(value="/history",method = RequestMethod.GET)
    public String list(HttpServletRequest request){
		String grafanaHost = PropertiesUtil.getValue("grafana.host");
		request.setAttribute("grafanaHost", grafanaHost);
        return "iot/data/history_list";
    }
	
	@RequestMapping(value="/current",method = RequestMethod.GET)
    public String current(HttpServletRequest request){
        return "iot/data/current_list";
    }

	
	@RequestMapping(value="/diff",method = RequestMethod.GET)
    public String diff(HttpServletRequest request){
		String grafanaHost = PropertiesUtil.getValue("grafana.host");
		request.setAttribute("grafanaHost", grafanaHost);
        return "iot/data/table_list";
    }

    @RequestMapping(value = "/history/loadData/{orgId}", method = RequestMethod.POST)
    @ResponseBody
    public Result loadHistroyData(@PathVariable("orgId") String orgId) {
        Result result = new Result();
        String grafanaHost = PropertiesUtil.getValue("grafana.host");
        Map<String, String> map = new HashMap<String, String>();
        map.put("grafanaHost", grafanaHost);
        if ("0".equals(orgId)) {
            try {
                String innerHtml = FreeMarkerUtil.getStringByTemplate("histroy_all.html", map);
                result.setSuccess(true);
                result.setData(innerHtml);
            } catch (Exception e) {
                e.printStackTrace();
                result.setSuccess(false);
            }
        } else {
            List<Device> devices = baseService.find("from Device where orgid='" + orgId + "' and type='采集器'");
            if (null != devices && !devices.isEmpty()) {
                Device device = devices.get(0);
                String qxzCode = device.getDevip() + device.getPort();
                map.put("dcode", qxzCode);
                try {
                    String innerHtml = FreeMarkerUtil.getStringByTemplate("histroy.html", map);
                    result.setSuccess(true);
                    result.setData(innerHtml);
                } catch (Exception e) {
                    e.printStackTrace();
                    result.setSuccess(false);
                }
            } else {
                result.setSuccess(false);
            }
        }
        return result;
    }
}
